<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Products_Model extends CI_Model {

    protected $_lang = '';

    function __construct() {
        parent::__construct();
    }

    public function initialise($lang) {
        if ($lang != '' && $lang != 'vn')
            $this->_lang = '_' . $lang;
        else
            $this->_lang = '';
    }

    /*
     * @author : Tran Thi Kim Que
     * @method : get product category from alias
     * @param  : $id_cat - ID category news
     * @date   : 17/05/2013
     * @modify :
     */

    function getProductCategoryAlias($alias) {
        $this->db->where('bl_active', 1);
        $this->db->where('name_alias', $alias);
        $result = $this->db->get('product_category');
        if ($result)
            return $result->row();
        return '';
    }

    function getAlias($id) {
        $this->db->where('bl_active', 1);
        $this->db->where('product_category_id', $id);
        $result = $this->db->get('product_category')->row();
        if ($result)
            return $result->name_alias;
        return '';
    }

    /*
     * @author : Tran Thi Kim Que
     * @method : get all product category
     * @param  : $id_cat - ID category news
     * @date   : 09/04/2013
     * @modify :
     */

    function getProductCategoryId($id) {
        $this->db->where('bl_active', 1);
        $this->db->where('product_category_id', $id);
        return $result = $this->db->get('product_category')->row();
    }

    /** lấy thông tin san phẩm dựa vào id cua san pham do ** */
    function getProductFromId($id) {
        $this->db->where('bl_active', 1);
        $this->db->where('products_id', $id);
        return $result = $this->db->get('products')->row();
    }
    
    function getProductFromCode($code) {
        $this->db->where('bl_active', 1);
        $this->db->where('sku', $code);
        return $result = $this->db->get('products')->row();
    }

    /** sản phẩm cùng danh muc*** */
    function getOtherFromCategory($product_category_id = 0, $id = 0) {
        $substring_procate = $this->getStringSubProductCategory($product_category_id);
		$arr_where_in = explode(',', $substring_procate);
        $this->db->select('products_id,code,image,name,title,name_alias,viewed,sprice,price');
        $this->db->where_in('product_category_id', $arr_where_in);
        $this->db->where('bl_active', 1);
        $this->db->where('products_id <> ', $id);
        return $this->db->get('products', 20, 0)->result();
    }
	function delete(){
			  $this->db->where('bl_active',1);
			  $query = $this->db->delete('products');
			  if($query){
				  return true;
			  }else{
				  return false;
			  }
	}
    /** random danh sách san pham*** */
    function getViewedProducts() {
        $this->db->select('products_id,code,image,name,title,name_alias,viewed,sprice,price');
        $this->db->where('bl_active', 1);
        $this->db->order_by('rand()');
        return $this->db->get('products', 24, 0)->result();
    }

    /* các danh sách sản phẩm
     * danh sách sản phẩm mới nhất thuộc 1 danh mục sản phẩm nào đó.
     */
    /*     * *** update lượt xem cua san pham ****** */

    function update_View($id, $view) {
        $this->db->where('products_id', $id);
        $data = array('viewed' => $view);
        $query = $this->db->update('products', $data);
        if ($query) {
            return true;
        } else {
            return false;
        }
    }
    
    function update_soluong($id, $soluong) {
        $this->db->where('products_id', $id);
        $data = array('soluong' => $soluong);
        $query = $this->db->update('products', $data);
        if ($query) {
            return true;
        } else {
            return false;
        }
    }
    
    function getSoluongFromId($id) {
        $this->db->where('bl_active', 1);
        $this->db->where('products_id', $id);
        $result = $this->db->get('products')->row();
        if(!empty($result)) {
            return $result->soluong;
        }
        return 0;
    }

    /*     * ***end *** */

    function getProductFromCategory($product_category = 0, $num = 20, $start = 0) {
        $substring_procate = $this->getStringSubProductCategory($product_category);
        $arr_where_in = explode(',', $substring_procate);
        $this->db->select('products_id,code,image,title,name,name_alias,viewed,sprice,price');
        $this->db->where_in('product_category_id', $arr_where_in);
        $this->db->where('bl_active', 1);
        $this->db->order_by('dt_create', 'DESC');
        return $this->db->get('products', $num, $start)->result();
    }

    // get string product_category_id;
    function getStringSubProductCategory($parent_id = 0) {
        $str_sub = array();
        $this->db->select('product_category_id');
        $this->db->where('parent_id', $parent_id);
        $this->db->where('bl_active', 1);
        $this->db->order_by('nb_order', 'ASC');
        $resultItems = $this->db->get('product_category')->result();
        if ($resultItems) {
            foreach ($resultItems as $sub_cate_id) {
                //$str_sub[] = $sub_cate_id->product_category_id;
                $next_sub = $this->getStringSubProductCategory($sub_cate_id->product_category_id);
                if (!empty($next_sub))
                    $str_sub[] = $next_sub;
            }
        }
        $str_sub[] = $parent_id;

        return implode(',', $str_sub);
    }

    // get object product_category
    // get string product_category_id;
    function getObjectSubProductCategory($parent_id = 0) {
        $this->db->where('parent_id', $parent_id);
        $this->db->where('bl_active', 1);
        $this->db->order_by('nb_order', 'ASC');
        $resultItems = $this->db->get('product_category')->result();
        return $resultItems;
    }

    // danh muc gốc
    function getRootParent($id) {
        $arrRoot = array();
        $objectCategorys = $this->getProductCategoryId($id);
        if ($objectCategorys) {
            $root_id = $objectCategorys->parent_id;
            if ($root_id != 0) {
                $arrRoot = $this->getRootParent($root_id);
            }
            $arrRoot[] = $objectCategorys;
        }
        return $arrRoot;
    }

    // lấy danh sach sản phẩm, phân trang
    function getProducts($where = array(), $order = '', $num = 20, $start = 0) {
        $this->db->select('products_id,code,image,name,name_alias,title,image,viewed,sprice,price,description');
        $this->db->where($where);
        if ($order)
            $this->db->order_by($order);
        return $this->db->get('products', $num, $start)->result();
    }
	function getProductsSale($where = array(), $order = '', $num = 20, $start = 0) {
        $this->db->select('products_id,code,image,name,name_alias,title,image,viewed,sprice,price,description');
        $this->db->where($where);
        if ($order)
            $this->db->order_by($order);
		$this->db->where('price <> sprice');
		$this->db->get('products', $num, $start);
        return $this->db->get('products', $num, $start)->result();
    }
    // tổng số mẫu tin
    function getNumProducts($where = array()) {
        $this->db->select('products_id,code,image,name,name_alias,title,image,viewed,sprice,price,description');
        $this->db->where($where);
        return $this->db->get('products')->num_rows();
    }
 function getNumProductsSale() {
        $this->db->select('products_id,code,image,name,name_alias,title,image,viewed,sprice,price,description');
        $this->db->where('price <> sprice');
        return $this->db->get('products')->num_rows();
    }
    function getNumProductsFromCate($product_category) {
        $substring_procate = $this->getStringSubProductCategory($product_category);
        $arr_where_in = explode(',', $substring_procate);
        $this->db->select('products_id');
        $this->db->where_in('product_category_id', $arr_where_in);
        $this->db->where('bl_active', 1);
        return $this->db->get('products')->num_rows();
    }

    // tìm kiếm theo giá
    function getProductsFromPrice($codition) {
        $this->db->select('products_id,code,image,name,name_alias,title,image,viewed,sprice,price,description');
        if (!empty($codition['price_from'])) {
            $this->db->where('price >=', $codition['price_from']);
        }
        if (!empty($codition['price_to'])) {
            $this->db->where('price <=', $codition['price_to']);
        }
        if (!empty($codition['q_search'])) {
            $this->db->like('name', $codition['q_search']);
            $this->db->or_like('title', $codition['q_search']);
            $this->db->or_like('description', $codition['q_search']);
            $this->db->or_like('meta_tags', $codition['q_search']);
        }
		$this->db->where('bl_active', 1);
        return $this->db->get('products')->result();
    }
	// tim kiếm theo tag
	 function getSearchProductsFromTags($codition) {
        $this->db->select('products_id,code,image,name,name_alias,title,image,viewed,sprice,price,description');
        if (!empty($codition['alias'])) {
            $this->db->like('name', $codition['alias']);
            $this->db->or_like('title', $codition['alias']);
            $this->db->or_like('description', $codition['alias']);
            $this->db->or_like('meta_tags', $codition['alias']);
        }
		$this->db->where('bl_active', 1);
        return $this->db->get('products')->result();
    }

}